package com.rui.study.algorithm.P_合并K个排序链表;

import com.rui.study.algorithm.ListNode;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        ListNode h = null;
        for (int i = 0; i < lists.length; i++) {
            h = merge2Lists(h, lists[i]);
        }
        return h;
    }


    public ListNode merge2Lists(ListNode a, ListNode b) {

        ListNode head = new ListNode(Integer.MIN_VALUE), tail = head;

        while (a != null && b != null) {
            if (a.val < b.val) {
                tail.next = a;
                tail = a;
                a = a.next;
            } else {
                tail.next = b;
                tail = a;
                b = b.next;
            }
        }

        if (a == null) {
            tail.next = b;
        }
        if (b == null) {
            tail.next = a;
        }
        return head.next;
    }

    public static void main(String[] args) {
        ListNode[] lists = new ListNode[]{};
    }
}

